﻿
namespace BaddiesEditor
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// Custom form for when a new map is requested.
    /// </summary>
    public partial class NewMap : Form
    {
        /// <summary>
        /// Handler for when the form finishes correctlly.
        /// </summary>
        private EventHandler acceptButtonHandler;

        /// <summary>
        /// Initializes a new instance of the NewMap class.
        /// </summary>
        /// <param name="acceptHandler">Handler for the ok button event.</param>
        public NewMap(EventHandler acceptHandler)
        {
            this.InitializeComponent();
            this.acceptButtonHandler += acceptHandler;
        }

        /// <summary>
        /// Handler for the Ok button.
        /// </summary>
        /// <param name="sender">Class that sends the message.</param>
        /// <param name="e">Arguments of the call.</param>
        private void OkButton_Click(object sender, EventArgs e)
        {
            NewMapConfirmArg args = new NewMapConfirmArg();

            // Width
            if (this.ConfirmTextBoxValues(this.mapWidthTextBox))
            {
                args.Width = this.StringToInt(this.mapWidthTextBox.Text);
            }
            else
            {
                this.ErrorForm("Width must be a number between 1 and 999!");
                return;
            }

            // Height
            if (this.ConfirmTextBoxValues(this.mapHeightTextBox))
            {
                args.Height = this.StringToInt(this.mapHeightTextBox.Text);
            }
            else
            {
                this.ErrorForm("Height must be a number between 1 and 999!");
                return;
            }

            // Height
            if (this.ConfirmTextBoxValues(this.tileSizeTextBox))
            {
                args.TileSize = this.StringToInt(this.tileSizeTextBox.Text);
            }
            else
            {
                this.ErrorForm("TileSize must be a number between 1 and 999!");
                return;
            }

            // Name
            if (this.mapName.Text != string.Empty)
            {
                args.Name = this.mapName.Text;
            }
            else 
            {
                this.ErrorForm("Write a name for the map.");
                return;
            }

            this.acceptButtonHandler(this, args);
            this.Close();
        }

        /// <summary>
        /// Handler for the Cancel button.
        /// </summary>
        /// <param name="sender">Class that sends the message.</param>
        /// <param name="e">Arguments of the call.</param>
        private void CancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Creates a new error form with the specified error.
        /// </summary>
        /// <param name="msg">Error to display.</param>
        private void ErrorForm(string msg)
        {
            ErrorForm error = new ErrorForm();
            error.SetErrorMsg(msg);
            error.Show();
        }

        /// <summary>
        /// Confirms if a textbox adjust to the specified requirements.
        /// </summary>
        /// <param name="box">Textbox to check.</param>
        /// <returns>True if it is, false otherwise.</returns>
        private bool ConfirmTextBoxValues(TextBox box)
        {
            string text = box.Text;

            // Check it's a number
            int num = 0;
            if (!int.TryParse(text, out num))
            {
                return false;
            }

            // Check valid range
            if (num < 1 || num > 999)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// Transforms a string that has been confirmed represents a number
        /// into a number.
        /// </summary>
        /// <param name="text">Text to translate.</param>
        /// <returns>Number in the text.</returns>
        private int StringToInt(string text)
        {
            int num = 0;
            int.TryParse(text, out num);
            return num;
        }
    }
}
